<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>进度条</title>
    <style>
        div{
            height: 30px;
            width: 0px;
            background: #4b7bec;
        }
        #list{
            display: flex;
            list-style-type: none;
            width: 100vw;
            flex-direction: column;
        }
        #list li {
            margin-top: 5px;
            width: 20vw;
            height: 30px;
            line-height: 30px;
            background-color: #fc5c65;
            color: #ffffff;
            font-size: 1em;
            font-weight: bold;
            text-indent: 10px;
        }
        #finish{
            font-weight: bold;
            font-size: 1em;
        }
    </style>
</head>
<body>
    <div id="loading"></div>
    <ul id="list">
    </ul>
    <p id="finish"></p>
    <script src="../js/promiseAjax.js"></script>

    <script type="text/javascript">

        async function sleep(ms = 2000){
            return new Promise(resolve => {
                setTimeout(resolve,ms);
            });
        }
        //请求用户信息
        async function query(name){
            await sleep(1000);
            return Ajax(`http://localhost:8888/promise/php/user.php?name=${name}`);
        }

        (async () => {
            let users = ["后盾人","向军","李四","王五","赵六"];
            let loading = document.getElementById('loading');
            let list = document.getElementById('list');
            let finish = document.getElementById('finish');
            for(let i = 0 ; i < users.length ; i++){
                //得到用户信息
                let user =  await query(users[i]); 
                let progress = (i+1) / users.length;
                loading.style.width = progress * 100 + "px";
                loading.innerHTML = progress * 100 + '%';
                let li = document.createElement('li');
                let name = document.createTextNode(user.name);
                li.appendChild(name);
                list.appendChild(li);
            }
            finish.innerHTML = '数据加载完毕........';

        })();
    </script>

</body>
</html>
